package com.ym.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ym.pms.dao.CommentsMapper;
import com.ym.pms.model.Comments;
import com.ym.pms.model.Contract;
import com.ym.pms.model.Listoffees;
import com.ym.pms.dao.ListoffeesMapper;
import com.ym.pms.service.ListoffeesServices;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ming
 * @since 2020-12-23
 */
@Service
public class ListoffeesServiceImpl extends ServiceImpl<ListoffeesMapper, Listoffees> implements ListoffeesServices {
    private final float EFee=100f;
    private final float WFee=100f;
    @Autowired
    CommentsMapper  commentsMapper;

    @Override
//    每月1号0点自动开始生成费用清单
    @Scheduled(cron = "0 0 0 1 * *")
    public void sendFee() {
        List<Contract> contracts = baseMapper.findContracts();
        for (Contract contract : contracts) {
            Listoffees listoffees = new Listoffees();
            listoffees.setLofUid(contract.getCoUid());
            listoffees.setLofCoid(contract.getCoId());
            listoffees.setLofPhoneno(contract.getCoPBName());
            listoffees.setLofEfee(EFee);
            listoffees.setLofWfee(WFee);
            listoffees.setLofCopcosts(contract.getCoPCosts());
            listoffees.setLofCorent(contract.getCoRent());
            listoffees.setLofTotal(EFee+WFee+contract.getCoRent()+contract.getCoPCosts());
            baseMapper.insert(listoffees);

            Comments comments = new Comments();
            comments.setUserId(contract.getCoUid());
            comments.setAdminId(0);
            comments.setTitle("缴费通知");
            comments.setIsRead(0);
            comments.setCreateTime(new Date());
            comments.setRank(2);
            comments.setContent("尊敬的用户，您好，本月您需缴纳费用如下：<br/>" +
                    "水费：￥"+WFee+"元,<br/>" +
                    "电费：￥"+EFee+"元，<br/>" +
                    "房租：￥"+contract.getCoRent()+"元，<br/>" +
                    "物业费：￥"+contract.getCoPCosts()+"元。<br/>" +
                    "请及时前往缴费处缴费，如果15天内未完成以上全部缴费，则视为违约合同处理，请注意时间");
            commentsMapper.add(comments);
        }
    }

    @Override
    public void findAll(Integer id, Page<Listoffees> page) {
        QueryWrapper<Listoffees> wrapper = new QueryWrapper<>();
        wrapper.eq("lof_uid",id);
        wrapper.orderByDesc("lof_sendTime");
        baseMapper.selectPage(page,wrapper);
    }


    @Override
    public void generateFee(Integer id) {
        Contract contract = baseMapper.findByContractId(id);
        Listoffees listoffees = new Listoffees();
        listoffees.setLofUid(contract.getCoUid());
        listoffees.setLofCoid(contract.getCoId());
        listoffees.setLofPhoneno(contract.getCoPBPNo());
        listoffees.setLofEfee(0f);
        listoffees.setLofWfee(0f);
//        获取今天是几号
        Calendar calendar = Calendar.getInstance();
        float today = calendar.get(Calendar.DAY_OF_MONTH);
        DecimalFormat df = new DecimalFormat("#.00");
//        收费按当月剩余天数百分比计算
        listoffees.setLofCorent(Float.valueOf(df.format((31-today)/31*contract.getCoRent())));
        listoffees.setLofTotal(Float.valueOf(df.format((31-today)/31*contract.getCoRent())));
        baseMapper.insert(listoffees);
    }

    @Override
    public void pay(Integer id) {
        Listoffees listoffees = baseMapper.selectById(id);
        listoffees.setLofPaystatus(1);
        baseMapper.updateById(listoffees);
    }
}
